package com.ple.app.sevlet;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


/**
 * Created by cov_neeg on 2016/12/28.
 */

public class JsonTools {
    /**
     * 获取对象的map集合
     */
    public static List<Map<String,Object>> getListmap(String key, String jsonString){
        List<Map<String,Object>> list=new ArrayList<Map<String, Object>>();
        try {
            JSONObject jsonObject=new JSONObject(jsonString);
            JSONArray jsonArray=jsonObject.getJSONArray(key);
            for(int i=0;i<jsonArray.length();i++){
                JSONObject jsonObject2=jsonArray.getJSONObject(i);
                Map<String,Object> map=new HashMap<String, Object>();
                 Iterator<String> iterator=jsonObject2.keys();
                while (iterator.hasNext()){
                    String json_key=iterator.next();
                    Object json_value=jsonObject2.get(json_key);
                    if(json_value==null)json_value="";
                    map.put(json_key,json_value);
                }//while
                list.add(map);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }
    /**
     *获取STring数组
     */
    public static List<String> getList(String key, String jsonString){
        List<String> list=new ArrayList<String>();
        String[] args=new String[]{};
        try {
            JSONObject jsonObject=new JSONObject(jsonString);
            JSONArray jsonArray=jsonObject.getJSONArray(key);
            for(int i=0;i<jsonArray.length();i++){
                String msg=jsonArray.getString(i);
                list.add(msg);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return list;
    }
}